/*
 * Copyright 2008 Lucifer Lu
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using SilverChart.Core.Configuration;
using SilverChart.Core.Service;

namespace SilverChart.Configuration {
    /// <summary>
    /// Description of Legend.
    /// </summary>
    [ServiceProvider(Singleton = false, ServiceName = "Configuration Items: Legend")]
    [ConfigurationItem(Configurations.ITEM_NAME_LEGEND, CreateForDefault = true, Parent = typeof(Configurations))]
    public class Legend : AbstractConfigurationItem {
        public const String ITEM_NAME_DIRECTION = "direction";
        public readonly static LegendDirection DefaultLegendDirection = LegendDirection.Horizontal;
        private ValueItemHelper<LegendDirection> direction;

        public const String ITEM_NAME_BORDERCOLOR = "border_color";
        public readonly static Color DefaultBorderColor = Color.FromArgb(0xAA, 0xBB, 0xBB, 0xBB);//"#AABBBBBB"
        private ValueItemHelper<Brush> borderColor;

        public const String ITEM_NAME_BORDERTHICKNESS = "border_thickness";
        public const Double DefaultBorderThickness = 1;
        private ValueItemHelper<Double> borderThickness;

        public const String ITEM_NAME_BORDERCORNERRADIUS = "border_corner_radius";
        public const Double DefaultBorderCornerRadius = 2;
        private ValueItemHelper<Double> borderCornerRadius;

        public const String ITEM_NAME_BACKGROUND = "background";
        public readonly static Color DefaultBackgroundColor = Color.FromArgb(0x88, 0xD8, 0xBF, 0xD8);//#88D8BFD8
        private ValueItemHelper<Brush> background;

        public const String ITEM_NAME_HORIZONTALOFFSET = "h_offset";
        public const Double DefaultHorizontalOffset = 0.0d;
        private ValueItemHelper<Double> horizontalOffset;

        public const String ITEM_NAME_VERTICALOFFSET = "v_offset";
        public const Double DefaultVerticalOffset = 0;
        private ValueItemHelper<Double> verticalOffset;

        public const String ITEM_NAME_OUTOFCHART = "out_of_chart";
        public const Boolean DefalutOutOfChart = false;
        private ValueItemHelper<Boolean> outOfChart;

        public const String ITEM_NAME_HORIZONTALALIGNMENT = "h_alignment";
        public readonly static HorizontalAlignment DefaultHorizontalAlignment = HorizontalAlignment.Right;
        private ValueItemHelper<HorizontalAlignment> horizontalAlignment;

        public const String ITEM_NAME_VERTICALALIGNMENT = "v_alignment";
        public readonly static VerticalAlignment DefaultVerticalAlignment = VerticalAlignment.Top;
        private ValueItemHelper<VerticalAlignment> verticalAlignment;

        public Legend(ConfigurationParser parser)
            : base(parser) {
            direction = new SimpleValueItemHelper<LegendDirection>(this, ITEM_NAME_DIRECTION, DefaultLegendDirection);

            borderColor = new BrushValueItemHelper(this, ITEM_NAME_BORDERCOLOR, DefaultBorderColor);
            borderThickness = new SimpleValueItemHelper<Double>(this, ITEM_NAME_BORDERTHICKNESS, DefaultBorderThickness);
            borderCornerRadius = new SimpleValueItemHelper<Double>(this, ITEM_NAME_BORDERCORNERRADIUS, DefaultBorderCornerRadius);

            background = new BrushValueItemHelper(this, ITEM_NAME_BACKGROUND, DefaultBackgroundColor);

            horizontalOffset = new SimpleValueItemHelper<Double>(this, ITEM_NAME_HORIZONTALOFFSET, DefaultHorizontalOffset);
            verticalOffset = new SimpleValueItemHelper<Double>(this, ITEM_NAME_VERTICALOFFSET, DefaultVerticalOffset);

            outOfChart = new SimpleValueItemHelper<Boolean>(this, ITEM_NAME_OUTOFCHART, DefalutOutOfChart);

            horizontalAlignment = new SimpleValueItemHelper<HorizontalAlignment>(this, ITEM_NAME_HORIZONTALALIGNMENT, DefaultHorizontalAlignment);
            verticalAlignment = new SimpleValueItemHelper<VerticalAlignment>(this, ITEM_NAME_VERTICALALIGNMENT, DefaultVerticalAlignment);
        }
        public LegendDirection Direction { get { return direction.Value; } }

        public Brush BorderColor { get { return borderColor.Value; } }
        public Double BorderThickness { get { return borderThickness.Value; } }
        public Double CornerRadius { get { return borderCornerRadius.Value; } }

        public Brush Background { get { return background.Value; } }

        public Double HorizontalOffset { get { return horizontalOffset.Value; } }
        public Double VerticalOffset { get { return verticalOffset.Value; } }

        public Boolean OutOfChart { get { return outOfChart.Value; } }

        public HorizontalAlignment HorizontalAlignment { get { return horizontalAlignment.Value; } }
        public VerticalAlignment VerticalAlignment { get { return verticalAlignment.Value; } }

        public const String LegendsPanelName = "legendsPanel";
        public Border Border {
            get {
                Border border = new Border();
                border.BorderBrush = BorderColor;
                border.BorderThickness = new Thickness(BorderThickness);
                border.CornerRadius = new CornerRadius(CornerRadius);
                border.Background = Background;

                StackPanel stackPanel = new StackPanel();
                stackPanel.Name = LegendsPanelName;
                stackPanel.Margin = new Thickness(3.0d, 2.0d, 3.0d, 2.0d);
                border.Child = stackPanel;
                return border;
            }
        }

        //public Canvas LegendCanvas(String name, Brush fillColor, Brush strokeColor) {
        //    String legendDef = String.Format(ResourcesHelper.Instance[_Legend],
        //                                     name);
        //    Canvas canvas = (Canvas)XamlReader.Load(legendDef);
        //    Rectangle rect = (Rectangle)canvas.FindName("LegendRect");
        //    rect.Fill = fillColor;
        //    rect.Stroke = strokeColor;
        //    return canvas;
        //}
        //private const String _Legend = "Legend";
    }
}
